<template>
  <div>
    <el-table
      :data="tableData"
      border
      style="width: 100%">

      <el-table-column
        prop="systemName"
        label="系统名称"
        width="180">
      </el-table-column>

      <el-table-column
        prop="serverName"
        label="服务器名称"
        width="180">
      </el-table-column>

      <el-table-column
        prop="beginTime"
        label="巡检开始时间"
        width="180">
      </el-table-column>

      <el-table-column
        prop="finishTime"
        label="巡检结束时间"
        width="180">
      </el-table-column>

      <el-table-column
        prop="state"
        label="执行状态"
        width="180">
        <template slot-scope="scope">
          <span v-if="scope.row.state == 1">未巡检</span>
          <span v-if="scope.row.state == 2">巡检中</span>
          <span v-if="scope.row.state == 3">已巡检</span>
        </template>
      </el-table-column>

      <el-table-column
        prop="questionCount"
        label="问题个数"
        width="180">
      </el-table-column>

      <el-table-column
        prop="seriousCount"
        label="严重个数">
      </el-table-column>
      <el-table-column
        header-align="center"
        label="操作"
        width="300px"
        align="center">
        <template slot-scope="scope">
              <el-button type="primary" @click="getSystemByDBId(scope.row)">报告查看</el-button>
              <el-button type="primary" @click="handleExport(scope.row)">分析结果导出</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <el-dialog
      v-if="sysDialogVisible"
      title="详细信息"
      :visible.sync="sysDialogVisible"
      width="60%">
      <SysDiaLog ref="childRef" :system="systemName" :beginTime="beginTime"></SysDiaLog>
      <span slot="footer" class="dialog-footer">
        <el-button @click="sysDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="sysDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>  window._interceptConfig = {
  collection: true, //整体开关
  click: true, //页面点击事件信息采集开关
  page: true, //页面跳转加载事件信息采集开关
  error: true, //异常信息采集开关
  http: true, //通讯信息采集开关
  name: "jyzdhywxxxtsdfx_n_prd.mart-ui-ds", //系统编号，被采集信息的系统名称。前端简称
  url: " http://apm.sd.sgcc.com.cn:19411/api/v2/spansWeb"//数据上报服务地址，根据实际情况输入
};
</script>
<!-- src -->
<script src=" http://25.41.37.249:19411/assets/js/intercept.min.js " async></script>
<script>
  import {queryStateReport2,getSystemByDBId} from "@/api/diagmanage/stateReport"
  import SysDiaLog from "./systemdialog"
  import moment from "moment";
  import tab from "@/plugins/tab";
  import {analyResultExport} from "@/api/diagmanage/databasereport"
  export default {

    components:{
      SysDiaLog
    },
    data() {
      return {

        systemName:'',
        beginTime:'',
        sysDialogVisible:false,

        tableData: [],
        formInline: {

        },
        currentPage:1,
        pageSize:10,
        total:0
      }
    },
    methods:{

      handleSizeChange(val){
        this.pageSize = val
        this.getList()
      },
      handleCurrentChange(val){
        this.currentPage = val
        this.getList()
      },

      getSystemByDBId(db){
        this.systemName = db.systemName
        if (db.questionCount > 0 || db.seriousState > 0){
          this.sysDialogVisible = true
          this.$nextTick(()=>{
            this.$refs.childRef.init(db.reportId,db.beginTime)

          })
        }else {
          this.sysDialogVisible = true
          this.$nextTick(()=>{
            this.$refs.childRef.init('00',db.beginTime,db.systemName)

          })
        }

      },

      getList(){
        let data= {
          pageSize: this.pageSize,
          pageNumber:this.currentPage
        }
        queryStateReport2(data).then(res=>{

          this.tableData=res.data.data
          this.total = res.data.total
          for (let item in this.tableData) {
            let i  = this.tableData[item]
            if (i.state === '2') {
              i.finishTime='-'
              i.questionCount='-'
              i.seriousCount='-'
            }
            if (i.state === '1') {
              i.beginTime='-'
              i.finishTime='-'
              i.questionCount='-'
              i.seriousCount='-'
            }
          }
        })
      },
      onSubmit(){

      },
      search(){

        this.$router.push('/diagmanage/indiaecharts')

      },
      handleExport(row) {
        if (row.questionCount > 0 || row.seriousState > 0){
          getSystemByDBId(row.reportId).then(res=>{
            let tableData = res.data
            let Btime =  row.beginTime
            tableData.forEach(x=>{
              if (row.beginTime.startsWith("2024")){
                Btime = moment(Btime).add(5,'minutes').format("YYYY-MM-DD HH:mm:ss")
                x.beginTime = Btime
              }else {
                x.beginTime = '-'
                x.seriousState = 2
                x.baseInfomation = '未开始'
              }
            })
            analyResultExport(row.reportId,tableData).then(res=>{
               //如果后端返回的result是进过Blob处理的，直接 window.URL.createObjectURL(result)，如果没有，就需要先实例化new Blod处理之后再window.URL.createObjectURL(blob)。
              let blob = new Blob([res]);
              let url = window.URL.createObjectURL(blob);
              let link = document.createElement('a');
              link.download = "状态巡检报告.xls";
              link.href = url;
              link.click();
            })
          })
        }


        // this.download('StateReportController/export', {
        //   ...this.queryParams
        // }, `状态巡检-${new Date().getTime()}.xlsx`)
      }

    },
    created(){

      this.getList()

    }
  }
</script>

<style scoped>
</style>
